﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;

namespace giaodien
{
    public partial class GDChinh : Form
    {
        public GDChinh()
        {
            InitializeComponent();
        }

        public void GDChinh_Load(object sender, EventArgs e)
        {
            datengaydat.Enabled = false;
            combogioitinh.Enabled = false;
            datengaysinh.Enabled = false;
            DataSet ds = new DataSet();
            phong_DTO p = new phong_DTO();
            ds = phong_BUS.loadthongtinphongvaolist();
            hienthithongtinphonglist(ds, listphong);
            ds = new DataSet();
            ds = khachhang_BUS.loadthongtinkhlist();
            hienthithongtinkhlist(ds, listthongtinkh);
        }

        public void hienthithongtinphonglist(DataSet ds, ListView danhsach)
        {
            danhsach.Items.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lvw = new ListViewItem();
                lvw.Text = ds.Tables[0].Rows[i][0].ToString();
                for (int j = 1; j < ds.Tables[0].Columns.Count; j++)
                {
                    if (ds.Tables[0].Rows[i][j].ToString() != "")
                        lvw.SubItems.Add(ds.Tables[0].Rows[i][j].ToString());
                    else
                        lvw.SubItems.Add("");
                }
             danhsach.Items.Add(lvw);
            }
        }
        void laysukienkhimousedownphong(ListView danhsachmousedown, object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                ListViewItem lvw = listphong.GetItemAt(e.X, e.Y);
                if (lvw != null)
                {
                    txtmap.Text = lvw.SubItems[0].Text;
                }
            }
        }

        public void hienthithongtinkhlist(DataSet ds, ListView danhsach)
        {
            danhsach.Items.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lvw = new ListViewItem();
                lvw.Text = ds.Tables[0].Rows[i][0].ToString();
                for (int j = 1; j < ds.Tables[0].Columns.Count; j++)
                {
                    if (ds.Tables[0].Rows[i][j].ToString() != "")
                    {
                        if (j == 3)
                            lvw.SubItems.Add(string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(ds.Tables[0].Rows[i][3].ToString()).ToShortDateString()));
                        else
                            lvw.SubItems.Add(ds.Tables[0].Rows[i][j].ToString());
                    }
                    else
                        lvw.SubItems.Add("");
                }
                danhsach.Items.Add(lvw);
            }
        }

        public string taomakhtudong(string automakh)
        {
            DataSet ds = new DataSet();
            string laymalonnhathientai = "";
            int i = 0;
            ds = khachhang_BUS.automakh();
            if (ds.Tables[0].Rows.Count != 0)
            {
                laymalonnhathientai = ds.Tables[0].Rows[0][0].ToString();
                i = int.Parse(laymalonnhathientai.Substring(2, 3).ToString());
                i++;
                if (i < 10)
                    return "KH00" + i.ToString();
                else
                    return "KH0" + i.ToString();
            }
            else
            {
                return "KH001";
            }
        }

        void laysukienkhimousedownkh(ListView danhsachmousedown, object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
            {
                ListViewItem lvw = listthongtinkh.GetItemAt(e.X, e.Y);
                if (lvw != null)
                {
                    string ngay = lvw.SubItems[3].Text;
                    txttenkhachang.Text = lvw.SubItems[1].Text;
                    txtcmnd.Text = lvw.SubItems[2].Text;
                    datengaysinh.Text = lvw.SubItems[3].Text;
                    combogioitinh.Text = lvw.SubItems[4].Text;
                    txtsdt.Text = lvw.SubItems[5].Text;
                    txtmap.Text = lvw.SubItems[6].Text;
                }
            }
        }
        private void GDChinh_MouseDown(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseDown(e);
        }

        private void GDChinh_MouseUp(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseUp();
        }

        private void GDChinh_MouseMove(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void nutdv_Click(object sender, EventArgs e)
        {
            dichvu frm = new dichvu();
            frm.Show();
            this.Hide();
        }

        private void button46_Click_1(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Bạn Có Muốn Thoát", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                this.Close();
                DangNhap frm = new DangNhap();
                frm.Show();
            }
        }

        private void button47_Click_1(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void nutdangxuat_Click(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Bạn có chắc muốn đăng xuất", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                this.Close();
                DangNhap frm = new DangNhap();
                frm.Show();
            }
        }

        private void nutqlkh_Click(object sender, EventArgs e)
        {
            QLKH frm = new QLKH();
            frm.Show();
            this.Hide();
        }

        private void nutqlnv_Click(object sender, EventArgs e)
        {
            QLNV frm = new QLNV();
            frm.Show();
            this.Hide();
        }

        private void nutthongke_Click(object sender, EventArgs e)
        {
            ThongKeDoanhThu frm = new ThongKeDoanhThu();
            frm.Show();
            this.Hide();
        }

        private void listphong_MouseDown(object sender, MouseEventArgs e)
        {
            laysukienkhimousedownphong(listphong, sender, e);
        }

        private void listthongtinkh_MouseDown(object sender, MouseEventArgs e)
        {
            laysukienkhimousedownkh(listthongtinkh, sender, e);
        }
        int key = -1;
        private void nutthem_Click(object sender, EventArgs e)
        {
            txttenkhachang.Text = "";
            txtsdt.Text = "";
            txtcmnd.Text = "";
            combogioitinh.Text = "";
            datengaysinh.Text = "";
            txtmap.Text = "";
            datengaydat.Text = "";
            datengaydat.Enabled = true;
            txttenkhachang.ReadOnly = false;
            txtsdt.ReadOnly = false;
            txtcmnd.ReadOnly = false;
            combogioitinh.Enabled = true;
            datengaysinh.Enabled = true;
            key = 0;
        }

        private void nutluu_Click(object sender, EventArgs e)
        {
            if (key == 0)
            {
                if ((txtcmnd.Text == "") || (txttenkhachang.Text == "") || (combogioitinh.Text == "") || (txtsdt.Text == ""))
                    MessageBox.Show("Vui lòng điền đầy đủ thông tin");
                else
                {
                    if (txtmap.Text == "")
                        MessageBox.Show("Vui lòng chọn mã phòng");
                    else
                    {
                        DataSet ds = new DataSet();
                        khachhang_DTO kh = new khachhang_DTO();
                        phong_DTO p = new phong_DTO();
                        kh.makh = "";
                        kh.makh = taomakhtudong(kh.makh);
                        kh.tenkh = txttenkhachang.Text;
                        kh.cmnd = int.Parse(txtcmnd.Text);
                        kh.ngaysinh = DateTime.Parse(datengaysinh.Text);
                        kh.gioitinh = combogioitinh.Text;
                        kh.sdt = txtsdt.Text;
                        kh.ngaydat = DateTime.Parse(datengaydat.Text);
                        kh.map = int.Parse(txtmap.Text);
                        p.map = int.Parse(txtmap.Text);
                        if (khachhang_BUS.themkhvaop(kh, p) == true)
                        {
                            MessageBox.Show("Đăt phòng thành công");
                            txttenkhachang.Text = "";
                            txtsdt.Text = "";
                            txtmap.Text = "";
                            txtcmnd.Text = "";
                            combogioitinh.Text = "";
                            txttenkhachang.ReadOnly = true;
                            txtsdt.ReadOnly = true;
                            txtmap.ReadOnly = true;
                            txtcmnd.ReadOnly = true;
                            combogioitinh.Enabled = false;
                            datengaysinh.Enabled = false;
                            datengaydat.Enabled = false;
                            ds = khachhang_BUS.loadthongtinkhlist();
                            hienthithongtinkhlist(ds, listthongtinkh);
                            ds = new DataSet();
                            ds = phong_BUS.loadthongtinphongvaolist();
                            hienthithongtinphonglist(ds, listphong);

                        }
                        else
                            MessageBox.Show("Đăt phòng không thành công");
                    }
                }
            }
        }

        private void nutxemthongtinloaiphong_Click(object sender, EventArgs e)
        {
            Form frm = new thongtinloaiphong();
            frm.Show();
        }

        private void txtsdt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void txtcmnd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void nuttim_Click(object sender, EventArgs e)
        {
            khachhang_DTO kh = new khachhang_DTO();
            DataSet ds = new DataSet();
            if (txttimkiem.Text == "")
                MessageBox.Show("Vui lòng điền số CMND vào");
            else
            {
                kh.cmnd = int.Parse(txttimkiem.Text);
                ds = khachhang_DAO.timkiemkhachhang(kh);
                hienthithongtinkhlist(ds, listthongtinkh);
            }
        }

        private void nuttraphong_Click(object sender, EventArgs e)
        {
            Form frm = new hoadon();
            frm.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form frm = new themxoasuaphong();
            frm.Show();
        }

        private void combogioitinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void nutthemphong_Click(object sender, EventArgs e)
        {
            Form frm = new themxoasuaphong();
            frm.Show();
            this.Hide();
        }
    }
}
